package com.itany.boot.service.Impl;

import com.itany.boot.entity.User;
import com.itany.boot.entity.UserExample;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.UserService;
import com.itany.boot.dao.UserMapper;
import com.itany.boot.exception.MD5ErrorException;
import com.itany.boot.util.MD5Util;
import com.itany.boot.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    
    @Override
    public void register(User user) {
        try {
            if (ParameterUtil.isNull(user.getLoginName())) {
                throw new IllegalArgumentException("登录名不能为空");
            }
            if (!user.getLoginName().matches("^[a-zA-Z0-9_]{4,20}$")) {
                throw new IllegalArgumentException("登录名只能包含字母、数字和下划线，长度为4-20位");
            }
            UserExample loginNameExample = new UserExample();
            loginNameExample.or().andLoginNameEqualTo(user.getLoginName());
            List<User> loginNameUsers = userMapper.selectByExample(loginNameExample);
            if (loginNameUsers != null && !loginNameUsers.isEmpty()) {
                throw new IllegalArgumentException("该登录名已被注册");
            }

            if (ParameterUtil.isNull(user.getPassword())) {
                throw new RequestParameterErrorException("密码不能为空");
            }

            if (user.getPassword().contains(" ") || !user.getPassword().matches("^[a-zA-Z0-9]{6,20}$")) {
                throw new IllegalArgumentException("密码不能包含空格和特殊字符，只能包含字母和数字，长度为6-20位");
            }

            if (ParameterUtil.isNull(user.getNickname())) {
                throw new RequestParameterErrorException("昵称不能为空");
            }

            if (user.getNickname().length() > 20) {
                throw new IllegalArgumentException("昵称长度不能超过20个字符");
            }

            if (ParameterUtil.isNull(user.getEmail())) {
                throw new RequestParameterErrorException("邮箱不能为空");
            }
            if (!user.getEmail().matches("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$")) {
                throw new IllegalArgumentException("邮箱格式不正确");
            }
            UserExample emailExample = new UserExample();
            emailExample.or().andEmailEqualTo(user.getEmail());
            List<User> emailUsers = userMapper.selectByExample(emailExample);
            if (emailUsers != null && !emailUsers.isEmpty()) {
                throw new IllegalArgumentException("该邮箱已被注册");
            }

            String password = MD5Util.md5(user.getPassword());
            user.setPassword(password);
            user.setRole("normal");
            user.setStatus(0);
            user.setCreateDate(new Date());
            userMapper.insertSelective(user);
        } catch (MD5ErrorException e) {
            throw new RuntimeException("用户注册失败：" + e.getMessage(), e);
        } catch (RequestParameterErrorException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void login(String loginName, String password) throws RequestParameterErrorException {
        if(ParameterUtil.isNull(loginName) || ParameterUtil.isNull(password)) {
            throw new RequestParameterErrorException("请求参数错误");
        }
    }
}
